<template>
	<div>
		<el-dialog :title="$t('action.add')" width="65%" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form" :model="form" :rules="rules" :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">

        <el-row >
          <el-col :span="15">
            <el-form-item label="物料编码" prop="materialNo">
              <el-input v-model="form.materialNo" autoComplete="off" readonly placeholder="自动填充物料编码">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="15">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" autoComplete="off" readonly placeholder="自动填充物料名称">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row >
          <el-col :span="15">
            <el-form-item label="仓库编码" prop="stockNo">
              <el-input v-model="form.stockNo" autoComplete="off" readonly placeholder="自动填充仓库编码">
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="15">
            <el-form-item label="仓库名称" prop="stockName">
              <el-input v-model="form.stockName" autoComplete="off" readonly placeholder="自动填充仓库名称">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row >
          <el-col :span="15">
            <el-form-item label="仓位编码" prop="locationNo">
              <el-input
                  v-model="form.locationNo"
                  autoComplete="off"
                  clearable
                  placeholder="请输入仓位编码"
                  @blur="getLocationDetail">
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="15">
            <el-form-item label="仓位名称" prop="locationName">
              <el-input v-model="form.locationName" autoComplete="off" readonly placeholder="自动填充仓位名称">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">{{$t('system.confirm')}}</span>
					<span v-else>{{$t('system.submitting')}}</span>
				</el-button>
				<el-button :size="size" @click="closeForm">{{$t('system.cancel')}}</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { add} from '@/api/app/moldinspect'



export default {
	name:'moldinspectadd',
	components: {
	   	   
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
		querys:{
			type:Object,
		},
	},
	data(){
		return {
		    dialog:{
          malList:[]
			},

			form: {
			   Id:'',
			   materialNo:'',
			   materialName:'',
			   stockNo:'',
			   stockName:'',
         locationNo:'',
         locationName:'',
			},
		    initial:{},   
			loading:false,
			rules: {
        locationNo: [
          { required: true, message: '仓位编码不能为空', trigger: 'blur' }
        ]
			}
		}
	},
	watch:{
		show(val){
			if(val){
			}
		}
	},
	methods: {
    // 获取仓位详情
    async getLocationDetail() {
      const locationNo = this.form.locationNo?.trim();
      if (!locationNo) {
        this.$message.warning('请输入仓位编码');
        return;
      }

      try {
        const res = await this.$axios.post('/app/MoldInspect/moldloadd', {
          locationNo
        });

        // 修复点1：安全访问 dataList
        const dataList = res.data.data.dataList || [];
        if (dataList.length > 0) {
          const detail = dataList[0];
          this.form = {
            ...this.form,
            materialNo: detail.materialNo || '',
            materialName: detail.materialName || '',
            stockNo: detail.stockNo || '',
            stockName: detail.stockName || '',
            locationName: detail.locationName || ''
          };
        } else {
          this.$message.warning('未查询到仓位信息');
          this.resetNonLocationFields();
        }
      } catch (error) {
        console.error('查询失败:', error);

        // 修复点2：正确处理错误信息
        const errorMsg = error.response?.data?.msg
            || error.message
            || '查询仓位信息失败';

        this.$message.error(errorMsg);
        this.resetNonLocationFields();
      }
    },
    // 重置非仓位编码字段
    resetNonLocationFields() {
      this.form.materialNo = '';
      this.form.materialName = '';
      this.form.stockNo = '';
      this.form.stockName = '';
      this.form.locationName = '';
    },
		open(){
		  if(this.querys){
		  }
		},
		submit(){
			this.$refs['form'].validate(valid => {
				if(valid) {
					add(this.form).then(res => {
						if(res.status == 200){
							this.$message({message: this.$t('system.success'), type: 'success'})
							this.$emit('refresh_list')
							this.closeForm()
						}
					}).catch(()=>{
						this.loading = false
					})
				}
			})
		},
		closeForm(){
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form']!==undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common.scss'
</style>
